<template>
  <div>
    <el-dialog title="更新" :model-value="updateDialogVisible" @open="handleOpen" :before-close="handleCancel"
               width="30%">
      <el-form :model="formData" :rules="rules" label-width="100px" ref="updateForm">
        <el-form-item label="系统编号:">
          <el-input v-model="formData.resource.resourceId" disabled></el-input>
        </el-form-item>
        <el-form-item label="资源名:" prop="resourceName">
          <el-input v-model="formData.resource.resourceName" ></el-input>
        </el-form-item>
        <el-form-item label="资源描述:" prop="resourceDescription">
          <el-input v-model="formData.resource.resourceDescription"></el-input>
        </el-form-item>
        <el-form-item label="标签:" prop="tag">
          <el-input v-model="formData.resource.tag"></el-input>
        </el-form-item>
        <el-form-item label="资源类型:" prop="resourceType">
          <el-input v-model="formData.resource.resourceType"></el-input>
        </el-form-item>
        <el-form-item label="用户id:" prop="userId">
          <el-input v-model="formData.resource.userId" disabled></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel()">取消</el-button>
        <el-button type="primary" @click="onSubmit()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup name='UpdateAuthority' lang='ts'>
import { ref, reactive } from 'vue'
import { queryTreeApi, listBackUriApi, modifyByIdApi, queryByIdApi } from '@/api/authorityApi';
import { ElMessage } from 'element-plus';
import {resourceApi} from "@/api/resourceApi";
// 新增权限对应的字段
let formData = reactive({
  resource: {
    resourceId: '',
    resourceType: '',
    resourceDescription: '',
    userId: '',
    resourceName: '',
    coverName:'',
    tag: ''
  },
})
let rules = reactive({
  resource:{
    resourceName: [
      { required: true, message: "请输入资源名称", trigger: "blur" },
    ],
    resourceDescription: [
      { required: true, message: "请输入资源描述", trigger: "blur" },
    ],
    tag: [
      { required: true, message: "请输入标签", trigger: "blur" },
    ],
    resourceType: [
      { required: true, message: "请输入资源类型", trigger: "blur" },
    ],
    userId: [
      { required: true, message: "请输入用户id", trigger: "blur" },
    ]
  },
})

let updateForm = ref()
const props = defineProps<{ updateDialogVisible: boolean, idToUpdate: string }>()
let emitter = defineEmits(["closeDialog", "updateSuccess"])
// 打开对话框事件
function handleOpen() {
  // 回显
  resourceApi.getbyId(Number(props.idToUpdate)).then(result => {
    Object.assign(formData.resource, result.data)
  })

}
function handleCancel() {
  updateForm.value.resetFields()
  emitter("closeDialog")
}
// 更新权限
function onSubmit() {
  updateForm.value.validate((valid: boolean) => {
    if (!valid) return
    // 执行逻辑
    resourceApi.updateResource(Number(formData.resource.resourceId), formData.resource).then(result => {
      ElMessage({ type: "success", message: "更新成功" })
      updateForm.value.resetFields()
      emitter("updateSuccess")
    })
  })
}
</script>
<style scoped></style>